package a_java_48_day.day6;

/**
 * 把字符串转换成整数
 */
public class Solution {
    public int StrToInt(String str) {
        if(str == null || str.equals("")){
            return 0;
        }
        char[] strArray = str.trim().toCharArray();
        int sum=0,flag=1,tmp=0;
        int first=0;
        if(strArray[0] == '-') {
            flag = -1;
            first=1;
        }
        if(strArray[0] == '+'){
            first=1;
        }
        for(int i =first;i < strArray.length;i++){
            if(strArray[i]>'9'||strArray[i]<'0'){
                return 0;
            }
            int dight = (int)(strArray[i]-'0');
            sum = sum*10+dight;
            if(flag ==1 && sum*10>Integer.MAX_VALUE - dight)
                return 0;
            if(flag == -1 && sum*10*flag < Integer.MIN_VALUE + dight)
                return 0;
        }
        return flag* sum;
    }
}